package com.lqm.home.widget;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.lqm.home.R;


public class QuickIndexBar extends View {

	private Paint mPaint;
	private static final String[] LETTERS = new String[] {"↑","A", "B", "C", "D",
			"E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
			"R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#" };
	private int cellWidth;
	private float cellHeight;
	
	//暴露一个字母的监听
	public interface OnLetterUpdateListener{
		void onLetterUpdate(String letter);
	}
	
	private OnLetterUpdateListener listener;
	

	public OnLetterUpdateListener getListener() {
		return listener;
	}

	public void setListener(OnLetterUpdateListener listener) {
		this.listener = listener;
	}

	public QuickIndexBar(Context context) {
		this(context, null);
	}

	public QuickIndexBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public QuickIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setColor(getResources().getColor(R.color.side_bar));
		mPaint.setTextSize(25f);
		//mPaint.setTypeface(Typeface.DEFAULT_BOLD);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		for (int i = 0; i < LETTERS.length; i++) {
			String text = LETTERS[i];
			// 计算坐标
			// x坐标为单元格宽度的一半 减去 文字宽度的一半
			int x = (int) (cellWidth / 2.0f - mPaint.measureText(text) / 2.0f);

			Rect bounds = new Rect();
			mPaint.getTextBounds(text, 0, text.length(), bounds);
			// 文本的高度
			int textHeight = bounds.height();

			// y坐标为单元格高度的一半 + 文字高度的一半 + 上面有多少个单元格的高度（index*cellHeight）
			int y = (int) (cellHeight / 2.0f + textHeight / 2.0f + i
					* cellHeight);
			
			//mPaint.setColor(touchIndex == i? Color.GRAY : Color.WHITE);//被选择到的字母变成灰色
			// 绘制文本A-Z,此处的x，y坐标是字母左下方的坐标
			canvas.drawText(text, x, y, mPaint);
		}
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);

		// 获取单元格的宽度和高度
		cellWidth = getMeasuredWidth();
		cellHeight = getMeasuredHeight() * 1.0f / LETTERS.length;
	}

	int touchIndex = -1;//用于记录当前触摸的索引值
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int index = -1;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			index = (int) (event.getY() / cellHeight);//  y值/每个单元格的高度 = 当前单元格的索引
			if(index >= 0 && index < LETTERS.length){
				if(index != touchIndex){
					if(listener != null){
						listener.onLetterUpdate(LETTERS[index]);
						touchIndex = index;
					}
				}
			}
			setBackgroundColor(getResources().getColor(R.color.side_bar_pressed));
			break;

		case MotionEvent.ACTION_MOVE:
			index = (int) (event.getY() / cellHeight);//  y值/每个单元格的高度 = 当前单元格的索引
			if(index >= 0 && index < LETTERS.length){
				if(index != touchIndex){
					if(listener != null){
						listener.onLetterUpdate(LETTERS[index]);
						touchIndex = index;
					}
				}
			}
			setBackgroundColor(getResources().getColor(R.color.side_bar_pressed));
			break;

		case MotionEvent.ACTION_UP:
			touchIndex = -1;
			setBackgroundColor(Color.TRANSPARENT);
			break;
		}
	
		//invalidate();//重新调用onDraw方法实现选中的字母更改颜色
		return true;
	}

}
